import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    Dimensions,
    ListView,
    TouchableOpacity,
    TextInput,
    AsyncStorage,
    StatusBar
} from 'react-native';
import StorageUtil from '../../views/StorageUtil';
import Config from '../../common/Config'
import Utils from '../../common/Utils'
import {Toast,ModalIndicator} from 'teaset';
const STORAGE_KEY_TOKEN = '@AsyncStorageDemo:key_token';
const {height, width} = Dimensions.get('window');
export default class RegistrationPasswordLoginPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            password:'',
            nickname:'',
            token:'',
            area:''
        }
    };

    register=()=>{
        this.topTextInput.blur();
        this.bottomTextInput.blur();
        let re = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)[0-9A-Za-z]{6,23}$/;
        let checkPassword = re.test(this.state.password);
        if(this.state.password===''){
            Toast.message('密码不能为空')
        }else if(this.state.nickname===''){
            Toast.message('昵称不能为空')
        }else if(checkPassword!==true){
            Toast.message('密码格式不正确')
        }else{
            let params={
                "phone":this.props.phone,
                "password":this.state.password,
                "nickname":this.state.nickname,
                "area":this.props.area
            };
            ModalIndicator.show("正在请求数据......");
            fetch(Config.baseApi+'/user/register',{
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: Utils.getParamsString(params)
            })
                .then((response) => response.json())
                .then((responseJson) => {
                    ModalIndicator.hide();
                    if(responseJson.status!==1){
                        this.setState({
                            token:responseJson.data.token,
                            area:responseJson.data.area
                        },()=>{
                            AsyncStorage.removeItem(STORAGE_KEY_TOKEN);
                            AsyncStorage.setItem(STORAGE_KEY_TOKEN,this.state.token);
                        });
                        Actions.EnquiriesTabPage({area:this.state.area})
                    }else{
                        Toast.message(responseJson.msg);
                    }
                })
                .catch((error) => {
                    ModalIndicator.hide();
                    Toast.message(error);
                });
        }
    };



    render() {
        return (
            <View style={styles.container}>
                <StatusBar
                    backgroundColor="white"
                    barStyle="dark-content"
                />
                <TouchableOpacity
                    onPress={()=>Actions.pop()}
                    style={styles.goBackView}>
                    <Image
                        source={ImageUtil.GoBack}
                        style={styles.goBackImg}/>
                </TouchableOpacity>
                <View style={styles.topTitle}>
                    <Text style={styles.topTitleMsg}>手机号注册</Text>
                </View>
                <View style={styles.passwordAndNicknameView}>
                    <View style={styles.areaView}>
                        <Text style={styles.areaMsg}>手机号</Text>
                        <View style={styles.phoneView}>
                            <Text style={styles.phoneMsg}>+86 {this.props.phone}</Text>
                        </View>
                    </View>
                    <View style={styles.topTextInputView}>
                        <Text style={styles.TextInputTitleMsg}>密码</Text>
                        <TextInput
                            ref={ (ref)=>this.topTextInput = ref }
                            autoCapitalize={"none"}
                            underlineColorAndroid={'transparent'}
                            defaultValue={this.state.password}
                            placeholder="请输入密码"
                            placeholderTextColor='#bababa'
                            autoCorrect={false}
                            secureTextEntry={true}
                            style={styles.textInputView}
                            onChangeText={(text)=>{
                                this.setState({
                                    password:text,
                                })
                            }}/>
                    </View>
                    <View style={styles.topTextInputView}>
                        <Text style={styles.TextInputTitleMsg}>昵称</Text>
                        <TextInput
                            ref={ (ref)=>this.bottomTextInput = ref }
                            autoCapitalize={"none"}
                            underlineColorAndroid={'transparent'}
                            defaultValue={this.state.nickname}
                            placeholder="请输入昵称"
                            placeholderTextColor='#bababa'
                            autoCorrect={false}
                            style={styles.textInputView}
                            onChangeText={(text)=>{
                                this.setState({
                                    nickname:text,
                                })
                            }}/>
                    </View>
                </View>
                <View style={styles.loginView}>
                    {this.state.password||this.state.nickname
                    ?(<TouchableOpacity
                            onPress={()=>this.register()}
                            style={[styles.loginButView,{backgroundColor:'#3090dd'}]}>
                            <Text style={styles.loginButMsg}>注册</Text>
                        </TouchableOpacity>)
                    :(<TouchableOpacity style={[styles.loginButView,{backgroundColor:'#69a9dd'}]}>
                            <Text style={styles.loginButMsg}>注册</Text>
                        </TouchableOpacity>)}
                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#ffffff'
    },
    goBackView:{
        position: 'absolute',
        left: 15,top:35
    },
    goBackMsg:{
        fontSize:16,
        color:'#3090dd',
        textAlign:'center'
    },
    topTitle: {
        marginTop: 109,
        justifyContent: 'center',
        alignItems: 'center',
    },
    topTitleMsg: {
        fontSize: 20,
        color: '#000000'
    },
    passwordAndNicknameView:{
        marginTop: 24,
        marginLeft: 30
    },
    areaView: {
        width: width - 60,
        flexDirection: 'row',
        alignItems: 'center',
        height: 40,
        borderColor: '#bababa',
        borderBottomWidth: 1
    },
    areaMsg: {
        fontSize: 15,
        color: '#000000'
    },
    phoneView:{
        position: 'absolute',
        left: 105,
        height: 40,
        width: width - 165,
        justifyContent:'center',
    },
    phoneMsg:{
        fontSize:15,
        color:'#000000',
    },
    topTextInputView:{
        width:width-60,
        flexDirection:'row',
        alignItems:'center',
        height:40,
        borderColor:'#bababa',
        borderBottomWidth:1
    },
    TextInputTitleMsg:{
        fontSize:15,
        color:'#000000'
    },
    textInputView: {
        position: 'absolute',
        left: 105,
        height: 40,
        width: width - 165,
        fontSize: 15
    },
    loginView:{
        marginTop:39,
        justifyContent:'center',
        alignItems:'center'
    },
    loginButView:{
        width:width-60,
        alignItems:'center',
        justifyContent:'center',
        borderRadius:5,
        height:44
    },
    loginButMsg:{
        color:'#ffffff',
        textAlign:'center',
        fontSize:17
    },
    ButMsg:{
        fontSize:13,
        color:'#3090dd'
    },
    ButView:{
        marginLeft:30,
        marginTop:28
    },
});
